package nioTest;

import java.io.FileInputStream;
import java.net.InetSocketAddress;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;

/**
 * @author      : yang.xp
 * @date        : 2022/3/16 0016 16:22
 * @description : 发送总的字节数：6155791，costTime = 16ms
 */
public class NewIoClient {

    public static void main(String[] args) throws Exception {
        SocketChannel socketChannel = SocketChannel.open();

        socketChannel.connect(new InetSocketAddress("localhost", 7001));
        String fileName = "D:\\ahemp-server.zip";

        FileInputStream fileInputStream = new FileInputStream(fileName);
        FileChannel fileInputStreamChannel = fileInputStream.getChannel();

        long startTime = System.currentTimeMillis();

        // linux 和 windows 下的 transferTo，windows单次只能传送 8M
        long transferCount = fileInputStreamChannel.transferTo(0, fileInputStream.available(), socketChannel);

        System.out.println("发送总的字节数：" + transferCount + "，costTime = " + (System.currentTimeMillis() - startTime) + "ms");
    }
}
